<template>
  <div class="app-container">
    <my-affix :offset="0" style="background: #fff;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="商品id" prop="id">
          <el-input v-model="queryParams.goodsId" placeholder="请输入盲盒id" clearable  />
        </el-form-item>
        <el-form-item label="产品名称" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入产品名称" clearable  />
        </el-form-item>
        <el-form-item label="商品状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择商品状态" >
            <el-option v-for="item in [
              {
                value: '3',
                label: '全部',
              },
              {
                value: '2',
                label: '下架',
              },
              {
                value: '1',
                label: '上架',
              },
            ]" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="库存" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入库存"
          clearable
          
        />
      </el-form-item> -->
        <!-- <el-form-item label="售卖数量" prop="saleNum">
        <el-input
          v-model="queryParams.saleNum"
          placeholder="请输入售卖数量"
          clearable
          
        />
      </el-form-item> -->
        <el-form-item label="产品价值" prop="price">
          <el-input v-model="queryParams.price" placeholder="请输入产品价值" clearable  />
        </el-form-item>
        <el-form-item label="回收价格" prop="money">
          <el-input v-model="queryParams.money" placeholder="请输入回收价格" clearable  />
        </el-form-item>
        <!-- <el-form-item label="第几套" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入第几套"
          clearable
          
        />
      </el-form-item> -->
        <el-form-item label="赏类型" prop="shangId">
          <el-select v-model="queryParams.shangId" clearable placeholder="请选择赏类型" @change="handleQuery">
            <el-option v-for="item in shang_option" :key="item.label" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable  />
        </el-form-item>
        <el-form-item label="真实概率" prop="realPro">
          <el-input v-model="queryParams.realPro" placeholder="请输入真实概率" clearable  />
        </el-form-item>
        <el-form-item label="显示概率" prop="lookPro">
          <el-input v-model="queryParams.lookPro" placeholder="请输入显示概率" clearable  />
        </el-form-item>
        <el-form-item label="保底赏概率" prop="minPro">
          <el-input v-model="queryParams.minPro" placeholder="请输入保底赏概率" clearable  />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">

        <el-tag :type="jisuan.lr<0?'danger':'success'">套内价格:{{ this.jisuan.sr }}</el-tag>
        <el-tag :type="jisuan.lr<0?'danger':''">抽赏总价:{{ this.jisuan.zc }}</el-tag>
        <el-tag :type="jisuan.lr<0?'danger':'success'">利润:{{ this.jisuan.lr }}</el-tag>
        <el-tag :type="jisuan.lr<0?'danger':''">利润率:{{ this.jisuan.lrl }}</el-tag>
      </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:yfs_list:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:yfs_list:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:yfs_list:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:yfs_list:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
  </my-affix>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange"
      @sort-change="dataListSortChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column sortable="custom" label="商品ID" align="center" prop="id" />
      <el-table-column label="盒子id" align="center" prop="goodsId" />
      <el-table-column label="产品名称" align="center" prop="title" />
      <el-table-column label="产品图片" align="center" prop="imgurl" width="120">
        <template slot-scope="scope">
          <el-image fit="cover" :src="scope.row.imgurl" style="width: 80px; height: 90px" />
        </template>
      </el-table-column>
      <el-table-column label="背景框" align="center" prop="bgImgurl">
        <template slot-scope="scope">
          <el-image :src="scope.row.bgImgurl" style="width: 80px; height: 90px" />
        </template>
      </el-table-column>
      <el-table-column label="赏分类" sortable="custom" align="center" prop="shangId">
        <template slot-scope="scope">
          {{ class_shang[scope.row.shangId] }}
        </template>
      </el-table-column>
      <el-table-column label="排序" sortable="custom" align="center" prop="sort" />
      <el-table-column label="库存" sortable="custom" align="center" prop="stock" />
      <!-- <el-table-column label="售卖数量" sortable="custom" align="center" prop="saleNum" /> -->
      <el-table-column label="产品价值" sortable="custom" align="center" prop="price" />
      <el-table-column label="回收价格" sortable="custom" align="center" prop="money" />
      <!-- <el-table-column label="第几套" sortable="custom" align="center" prop="num" /> -->

      <!-- <el-table-column label="真实概率" sortable="custom" align="center" prop="realPro" />

      <el-table-column label="保底赏概率" sortable="custom" align="center" prop="minPro" />
      <el-table-column label="显示概率" sortable="custom" align="center" prop="lookPro" /> -->
      <el-table-column label="状态" align="center" prop="status" sortable="custom">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 2">下架</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3">删除</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column sortable="custom" label="1普通 2无限" align="center" prop="goodsType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.goodsType == 1">普通</el-tag>
          <el-tag type="success" v-if="scope.row.goodsType == 2">无限</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column sortable="custom" label="是否参与熔铸" align="center" prop="ifAlchemy">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.ifAlchemy == 0">否</el-tag>
          <el-tag type="success" v-else>{{ scope.row.ifAlchemy }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column sortable="custom" label="是否参与排行榜" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.ifCollect == 0">否</el-tag>
          <el-tag type="success" v-else>{{ scope.row.ifCollect }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180" sortable="custom">
        <template slot-scope="scope">
          <span>{{
        scope.row.createDate
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:yfs_list:edit']">修改</el-button>

          <el-button size="mini" type="text" style="color:#F78989" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['system:yfs_list:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      :pageSizes="[20, 50, 100]" @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!-- 快捷导入 -->
      <el-button v-if="form.id == null" style="margin-bottom: 20px" type="primary" @click="importExcel">快捷导入</el-button>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="盲盒id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入盲盒id" />
        </el-form-item> -->
        <el-form-item  label="产品名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item  label="状态">
          <el-radio-group v-model.number="form.status">
            <el-radio :label="1">上架</el-radio>
            <el-radio :label="2">下架</el-radio>
            <el-radio :label="3">删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item  label="产品图片" prop="imgurl">
          <image-upload :limit="1" v-model="form.imgurl" />
        </el-form-item>
        <el-form-item  label="背景框" prop="bgImgurl">
          <image-upload :limit="1" v-model="form.bgImgurl" />
        </el-form-item>
        <el-form-item  label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>

        <el-form-item  label="产品价值" prop="price">
          <el-input v-model="form.price" :disabled="true" placeholder="请输入产品价值" />
        </el-form-item>
        <el-form-item  label="回收价格" prop="money">
          <el-input v-model="form.money" placeholder="请输入回收价格" />
        </el-form-item>

        <el-form-item  label="赏ID" prop="shangId">
          <el-select v-model="form.shangId" clearable placeholder="请选择赏类型">
            <el-option v-for="item in shang_option" :key="item.label" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item  label="是否参与熔铸" prop="ifAlchemy">
          <el-switch v-model.number="form.ifAlchemy" active-color="#13ce66" inactive-color="#F4F4F5" :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>

        <el-form-item  label="是否参与收集排行榜" prop="ifLs">
          <el-input v-model.number="form.ifCollect" placeholder="请输入收集" />
        </el-form-item>

        <el-form-item  label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <!-- <el-form-item label="真实概率" prop="realPro">
          <el-input v-model="form.realPro" placeholder="请输入真实概率" />
        </el-form-item>
        <el-form-item label="显示概率" prop="lookPro">
          <el-input v-model="form.lookPro" placeholder="请输入显示概率" />
        </el-form-item>
        <el-form-item label="保底赏概率" prop="minPro">
          <el-input v-model="form.minPro" placeholder="请输入保底赏概率" />
        </el-form-item> -->

        <!-- <el-form-item label="${comment}" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createDate">
          <el-date-picker
            clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}"
          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="up_end" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 快捷导入盒子商品 -->
    <el-dialog title="快捷导入" :visible.sync="dialogVisible" width="80%" center>
      <importForm @func="update" />
    </el-dialog>

    <!-- 快捷导入玩家自制盒子商品 -->
    <el-dialog title="快捷导入" :visible.sync="dialogVisible_user" width="80%" center>
      <importFormUser @update="import_user" :goodsId="user_goods_id" />
    </el-dialog>
  </div>
</template>

<script>
import myAffix from "@/components/affix.vue"
import {
  listBoxGoods,
  listList,
  getList,
  delList,
  addList,
  updateList,
  getType,
} from "@/api/yfs/add_good.js";
import importForm from "@/components/importForm";
import importFormUser from "@/views/czz/box_list/upfrom.vue";
export default {
  name: "AddBoxGoods",
  data() {
    return {
      jisuan: {
        sr: 0,
        cb: 0,
        lr: 0,
        lrl: 0,
      },
      price: null,

      //是否显示按钮dialog
      dialogVisible_user: false,
      up_end: false,
      //是否为玩家自制
      if_user_made: 0,
      user_goods_id: null,

      //是否打开快捷导入
      dialogVisible: false,
      //是否显示一键导入按钮
      showImport: false,
      shang_option: [],
      //赏类型对象
      class_shang: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc: "desc", // 排序，asc／desc
        orderByColumn: "updateTime", // 排序，字段
        box_id: null,
        pageNum: 1,
        pageSize: 100,
        goodsId: null,
        title: null,
        imgurl: null,
        status: null,
        stock: null,
        saleNum: null,
        price: this.price,
        money: null,
        num: 1,
        shangId: null,
        addtime: null,
        sort: null,
        realPro: null,
        lookPro: null,
        minPro: null,
        goodsType: null,
        type: null,
        bgImgurl: null,
        creator: null,
        createDate: null,
        ifCollect: 1,
        ifAlchemy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stock: [{ required: true, message: "请输入库存", trigger: "blur" }],
        title: [
          {
            required: true,
            message: "请输入赏品名称",
            trigger: "blur",
          },
        ],

        shangId: [
          { required: true, message: "请选择赏类型", trigger: "change" },
        ],
        goodsId: [
          { required: true, message: "盲盒id不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        realPro: [
          { required: true, message: "真实概率不能为空", trigger: "blur" },
        ],
        lookPro: [
          { required: true, message: "显示概率不能为空", trigger: "blur" },
        ],
        money: [
          { required: true, message: "回收价值不能为空", trigger: "blur" },
        ],
        goodsType: [
          { required: true, message: "1普通 2无限不能为空", trigger: "change" },
        ],
        type: [
          { required: true, message: "0普通 1特殊不能为空", trigger: "change" },
        ],
        price: [
          { required: true, message: "产品价值不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    importForm,
    importFormUser,
    myAffix
  },
  created() {
    this.queryParams.goodsId = this.$route.params.boxId.split("-")[0] || null;
    this.if_user_made = this.$route.params.boxId.split("-")[1];
    this.price = parseInt(this.$route.params.boxId.split("-")[2]);
    console.log(this.if_user_made);
    this.queryParams.box_id = this.$route.params.boxId;

    this.getType();
    this.getList();

    var _this = this;
    document.onkeydown = function (e) {
      let key = window.event.keyCode;
      if (key == 13) {
        _this.submitForm();
      }
    };
  },
  methods: {
    //计算预估价格
    jisuan_price(arr) {
      this.jisuan.sr = 0;
      this.jisuan.zc = 0;
      this.jisuan.lr = 0;
      this.jisuan.lrl = 0;
      console.log(arr);

      for (let i = 0; i < arr.length; i++) {
        this.jisuan.sr += arr[i].money * arr[i].stock;
        console.log(this.jisuan.sr);
        if (arr[i].shangId > 4) {
          this.jisuan.zc += arr[i].stock * this.price;
          console.log(this.jisuan.zc);
        }
      }
      this.jisuan.lr = this.jisuan.zc - this.jisuan.sr;
      //保留两位小数

      this.jisuan.lrl = ((this.jisuan.lr / this.jisuan.zc) * 100).toFixed(2) + '%';
    },

    //接收一键导入表单数据
    update(value) {
      console.log(value);
      this.dialogVisible = false;

      this.form = value;
      this.form.saleNum = 0;
      this.form.id = null;

      console.log('dddd');
      this.form.goodsId = this.queryParams.goodsId;
    },

    //导入玩家自制
    import_user(value) {
      console.log(value);

      this.form = value;
      this.form.saleNum = 0;
      this.form.id = null;
      // this.form.stock=2,

      this.form.price = this.price

      this.form.goodsId = this.queryParams.goodsId;
      // this.form.shangId = 'value.shangId';

      this.dialogVisible_user = false;
    },

    //快捷导入

    importExcel() {
      if (this.if_user_made == 1) {
        this.dialogVisible_user = true;
      } else {
        this.dialogVisible = true;
      }

      console.log("弹窗");
    },

    //获取赏类型
    getType() {
      getType().then((res) => {
        console.log(res.rows);
        this.typeList = res.rows;
        this.typeList.forEach((item) => {
          this.class_shang[item.id] = item.title;
        });
        this.shang_option = this.typeList.map((item) => {
          return {
            value: item.id,
            label: item.title,
          };
        });
        console.log(this.class_shang);
      });
    },

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;

      listBoxGoods(this.queryParams).then((response) => {
        this.listList = response.rows;
        this.total = response.total;
        this.loading = false;

        this.jisuan_price(this.listList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commonId: null,
        id: null,
        goodsId: this.queryParams.goodsId,
        title: null,
        imgurl: null,
        status: 2,
        stock: null,
        saleNum: null,
        price: this.price,
        money: null,
        num: null,
        shangId: null,
        addtime: null,
        sort: null,
        realPro: null,
        lookPro: null,
        minPro: null,
        goodsType: null,
        type: null,
        bgImgurl: null,
        updateTime: null,
        creator: null,
        createDate: null,
        ifAlchemy: 0
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.orderByColumn = "";
      this.queryParams.isAsc = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.commonId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(e) {
      if (this.if_user_made == 0) {
        this.showImport = true;
        this.reset();
        this.open = true;
        this.title = "添加盒子商品";
      } else {
        this.user_goods_id = this.queryParams.goodsId;
        this.showImport = true;
        this.dialogVisible_user = true;
        this.reset();

        this.open = true;


        this.title = "添加玩家自制盒子商品";
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.showImport = false;
      this.reset();
      const id = row.id || this.ids;
      getList(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改盒子商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.up_end = true;

          if (this.form.id != null) {
            updateList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.up_end = false;
              this.getList();
            });
          } else {
            this.form.creator = this.$store.getters.id;
            addList(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.up_end = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.commonId || this.ids;
      this.$modal
        .confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？')
        .then(function () {
          return delList(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/list/export",
        {
          ...this.queryParams,
        },
        `list_${new Date().getTime()}.xlsx`
      );
    },
    //排序
    dataListSortChangeHandle(data) {
      if (!data.order || !data.prop) {
        this.queryParams.orderByColumn = "";
        this.queryParams.isAsc = "";
        return false;
      }
      this.queryParams.isAsc = data.order.replace(/ending$/, "");
      this.queryParams.orderByColumn = data.prop
        .replace(/([A-Z])/g, "_$1")
        .toLowerCase();
      this.getList();
    },
    handleEditTable(row) {
      const tableId = row.id;
      const tableName = row.title;
      //   const params = { pageNum: this.queryParams.pageNum };
      console.log(row);
      this.$tab.openPage(
        "查看[" + tableName + "]盒子所有商品",
        "/box/goods_xq/index/" + tableId
      );
    },
  },
};
</script>
